from core.models.task import Task, UserTask
from django.shortcuts import render, get_object_or_404
from datetime import date
from django.shortcuts import redirect
from core.forms.task import TaskForm
from django.contrib.auth.decorators import login_required
from core.models.subject import Subject
from chtozadali.core.views.utilits import get_subjects

@login_required
def list_tasks(request):
  task_list = []
  for i in Task.objects.all().order_by("to_date").filter(user=request.user):
      i.state = i.usertask_set.get(user=request.user).is_done
      task_list.append(i)
  return render(request, "task_list.html", {"task_list":task_list,
                                            "subjects":get_subjects(Subject.objects.all()),
                                           })
@login_required
def list_tasks_to_day(request, day = date.today):
  task_list = Task.objects.filter(to_date = day).order_by("to_date")
  return render(request, "task_list.html", {"task_list":task_list,
  											"subjects":get_subjects(Subject.objects.all()),
                                           })
@login_required
def list_tasks_from_day(request, day = date.today):
  task_list = Task.objects.filter(from_date = day).order_by("to_date")
  return render(request, "task_list.html", {"task_list":task_list,
                                            "subjects":get_subjects(Subject.objects.all()),
                                           })
@login_required
def list_tasks_by_subject(request, sid):	
  task_list = Task.objects.filter(subject__id=sid).order_by("to_date")
  return render(request, "task_list.html", {"task_list":task_list,
                                            "subjects": get_subjects(Subject.objects.all()),
                                           })
@login_required
def detail_task(request, task_id):
  task = get_object_or_404(Task, pk=task_id)
  utask = task.usertask_set.get(task=task)
  return render(request, "task_detail.html", {"task": task,
                                              "subjects": get_subjects(Subject.objects.all()),
                                              "state": utask.is_done
                                             })


@login_required
def add_task(request):
    if request.method == "POST":
        task_form = TaskForm(request.POST)
        if task_form.is_valid():
            task = Task(
                         subject = task_form.cleaned_data["subject"],
                         from_date = task_form.cleaned_data["from_date"],
                         to_date = task_form.cleaned_data["to_date"],
                         title = task_form.cleaned_data["title"],
                         text = task_form.cleaned_data["text"],
                         task_type = task_form.cleaned_data["task_type"],
                        )
            task.save()
            priority = request.POST.get("priority", 10)
            task_user = UserTask(
                                 user=request.user,
                                 task=task,
                                 priority=priority
                                )
            task_user.save()
            return redirect("/")
    else:
        task_form = TaskForm()
    return render(request, "add_task.html", {"task_form": task_form,
                                             "subjects": get_subjects(Subject.objects.all()),
        })
